import {PluginCommand} from "./PluginCommand";
import {
  PluginCADToolFunctionType,
  PluginEventType
} from "../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";
import {ElementType} from "../modeling/TypeDefine";

class FastenerDesignCommand extends PluginCommand {
  constructor() {
    super();
  }
  //订阅拾取实例事件
  //参数：cb:回调函数
  handlePickInstance(cb){
    if (this.request) {
      this.request.subscribeEvent(PluginEventType.INSTANCEPICK, (e) => {
        let resultIns = e.instance;
        let insList = [];
        for (let i = 0; i < resultIns.length; i++) {
          if ([ElementType.DATUMPOINT, ElementType.POINT].includes(resultIns[i]?.type)) {
            let element = Object.assign({}, resultIns[i]);
            insList.push(element);
            cb(insList);
          } else if (resultIns[i]?.elementId > 0) {
            let ins = Object.assign({}, resultIns[i]);
            ins.name = resultIns[i]?.elementName + "@" + resultIns[i]?.name;
            insList.push(ins);
            cb(insList);
          }
        }
      });
    }
  }
  //发送消息处理
  axiosRequest(params, cb) {
    if (this.toolQuery) {
      this.toolQuery.axiosRequest(params)
        .then(res => {
          if (res.code === 200) {
            cb(res.data);
          }
        });
    }
  }
  //执行紧固件命令
  executeFastenerCommand(params) {
    if (this.request) {
      this.request.execFunction(PluginCADToolFunctionType.EXECUTEFASTENERCOMMAND, {params});
    }
  }
}

const fastenerDesignCommand = new FastenerDesignCommand();
export {fastenerDesignCommand}